<template>
  <div class="inventoryPosting">
    <div class="header">
      <div class="header-name">
        <span>{{ $t('market.fdate') }}</span>
        <el-date-picker
          v-model="fdate"
          type="month"
          value-format="yyyy-MM-dd"
          size="mini"
          style="width: 152px"
        />
        <span>{{ $t('market.deptClassify') }}</span>
        <el-input v-model.trim="workShopName" size="mini" style="width: 180px" />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
        <!--        <filtration :ftype="ftype" :msg="tacitly" :select-data="selectData" :table-header="tableHeader1" @seachData="handleQuerySonClass" />-->
      </div>
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
        sortable
        :get-summaries="getSummaries"
        show-summary
      >
        <el-table-column :label="$t('market.period')" prop="期间" align="center" width="60" />
        <el-table-column :label="$t('market.deptClassify')" prop="部门分类" align="center" />
        <el-table-column :label="$t('market.category')" prop="存货类型" align="center" />
        <el-table-column :label="$t('market.beginNum')" prop="期初数量" align="center" />
        <el-table-column :label="$t('market.beginPrice')" prop="期初金额" align="center" />
        <el-table-column :label="$t('market.benRukufqty')" prop="本期入库数量" align="center" />
        <el-table-column :label="$t('market.benRukuAmount')" prop="本期入库金额" align="center" />
        <el-table-column :label="$t('market.monthDelivery')" prop="本月出库" align="center">
          <el-table-column :label="$t('market.bqscllsl')" prop="本期生产领料数量" align="center" />
          <el-table-column :label="$t('market.bqscllje')" prop="本期生产领料金额" align="center" />
          <el-table-column :label="$t('market.bqyfllsl')" prop="本期研发领料数量" align="center" />
          <el-table-column :label="$t('market.bqyfllje')" prop="本期研发领料金额" align="center" />
          <el-table-column :label="$t('market.bqwwllsl')" prop="本期委外领料数量" align="center" />
          <el-table-column :label="$t('market.bqwwllje')" prop="本期委外领料金额" align="center" />
          <el-table-column :label="$t('market.bqqtllsl')" prop="本期非生研领料数量" align="center" />
          <el-table-column :label="$t('market.bqqtllje')" prop="本期非生研领料金额" align="center" />
        </el-table-column>
        <el-table-column :label="$t('market.finalNum')" prop="期末数量" align="center" />
        <el-table-column :label="$t('market.finalAmount')" prop="期末金额" align="center" />
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
  </div>
</template>

<script>
import {
  queryStockTotalList
} from '@/api/financial/salesMargin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel2'
import searData from '@/components/Search/mixin'
export default {
  name: 'OtherDetail',
  mixins: [searData],
  data() {
    return {
      fdate: '',
      workShopName: '',
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      tableHeight: '73vh',
      // 表头
      tableData: [], // 销售数据
      tableHeader: [],
      tableHeader1: []
    }
  },
  created() {
    // this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const multiHeader = [['期间', '部门分类', '存货类型', '期初数量', '期初金额', '本期入库数量', '本期入库金额', '本月出库',
          '', '', '', '', '', '', '', '期末数量', '期末金额'],
        [
          '', '', '', '', '', '', '', '本期生产领料数量',
          '本期生产领料金额', '本期研发领料数量', '本期研发领料金额', '本期委外领料数量', '本期委外领料金额', '本期非生研领料数量',
          '本期非生研领料金额', '', ''
        ]]
        const tHeader = []
        const filterVal = [
          '期间', '部门分类', '存货类型', '期初数量', '期初金额', '本期入库数量', '本期入库金额', '本期生产领料数量',
          '本期生产领料金额', '本期研发领料数量', '本期研发领料金额', '本期委外领料数量', '本期委外领料金额', '本期非生研领料数量',
          '本期非生研领料金额', '期末数量', '期末金额'
        ]
        const merges = [
          'H1:O1', 'A1:A2', 'B1:B2', 'C1:C2', 'D1:D2',
          'E1:E2', 'F1:F2', 'G1:G2', 'P1:P2', 'Q1:Q2'
        ]
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          queryStockTotalList({
            date: this.fdate,
            WorkShopName: this.workShopName,
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array
              const data = formatJson(filterVal, list)
              export_json_to_excel({ multiHeader, header: tHeader, data, merges, filename: '材料进销存明细汇总表' })
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取财务报表数据
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        date: this.fdate,
        WorkShopName: this.workShopName,
        pageNum: this.currentPage,
        pageSize: this.size
        // search: this.searCollData.concat(this.details)
      }
      const { data: RES } = await queryStockTotalList(DATA)
      this.tableData = RES.array
      this.total = RES.total
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.inventoryPosting {
  @include listBom;
  padding: 0;
  .header{
    position:relative;
    .header-name{
      width: 100%;
      span {
        width: 100px;
        font-size: 12px;
        text-align: center;
      }
    }
    .btn{
      transform: translateY(18%);
      margin-left: 15px !important;
    }
  }
}
</style>
